﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using DevComponents.WPF.Controls;
using DevComponents.WPF.Metro;
using Microsoft.Practices.Prism.Commands;
using ZhiHeng.OneCard.WpfClient.Forms;

namespace ZhiHeng.OneCard.WpfClient.ViewModel
{
    public class ApplicationViewModel : ViewModelBase
    {
        
        public MainViewModel MainViewModel
        {
            get { return MainViewModel.Instance; }
        }

        private object _description;
        public object Description
        {
            get { return _description; }
            set { SetPropertyValue(value, ref _description, "Description"); }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetPropertyValue(value, ref _title, "Title"); }
        }

        public MetroTileType TileType { get; set; }

        private MetroTheme? _metroTheme;
        public MetroTheme? MetroTheme
        {
            get { return _metroTheme; }
            set
            {
                SetPropertyValue(value, ref _metroTheme, "MetroTheme");
            }
        }

        public ImageSource ImageSource { get; set; }
            
        public string TileColor { get; set; }
        private string _width;
        public string Width { get { return _width; } set { _width = value; } }
        private string _height;
        public string Height { get { return _height; } set { _height = value; } }
        private bool _isActive;
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                SetPropertyValue(value, ref _isActive, "IsActive");
                if (this.MainViewModel != null)
                {
                    MainViewModel.CurrentApplication = this;
                }
                ChangeSubViewModelStatus();
            }
        }

        public string ViewPath { get; set; }

        private ViewModelBase _SubViewModel;
        private UserControl _subform;
        public UserControl SubForm 
        { 
            set
            {
                this.SetPropertyValue(value, ref _subform, "SubForm");
            }
            get
            {
                //if (_subform == null)
                //{
                    _subform = Activator.CreateInstance(Type.GetType(ViewPath)) as UserControl;
                    if (_subform != null) 
                        _SubViewModel = _subform.DataContext as ViewModelBase;
                    ChangeSubViewModelStatus();
                //}


                return _subform;
            }
        }

        private void ChangeSubViewModelStatus()
        {
            if (_SubViewModel != null)
            {
                if (!_isActive)
                {
                    _SubViewModel.OnUnActivated();
                }
                else
                {
                    _SubViewModel.OnActivated();
                }
            }
        }

        private DelegateCommand _ActivateCommand;
        public DelegateCommand ActivateCommand
        {
            get
            {
                if (_ActivateCommand == null)
                {
                    if(this.MainViewModel!=null)
                        _ActivateCommand = new DelegateCommand(() => IsActive = true, () => IsActive == false);
                }
                return _ActivateCommand;
            }
        }

        public DelegateCommand ReturnToStartCommand
        {
            get { return MainViewModel.Instance.ReturnToStartCommand; }
        }

    }
}
